//
//  CustomRequestAPI.swift
//  XGamesLive
//
//  Created by ios on 2024/5/16.
//

import Foundation
import PanKit
import Moya
import SwiftyRSA

class CustomRequestAPI: PanRequestBaseAPI,XGL_RequestConfigurationProtocol {
    enum CustomAPI{
        case Upload(image: UIImage,file: String)
        case UploadImages(images: [UIImage])
        case UploadVideo(video: Data)
        
        case Gifts
        case SendGift(giftId: String,userId: String)
        
        case fakeRequest(strContent: String?,intContent: NSInteger?)
        
        case rechargeList
        case rechargeWithAppleCheck(billNo: String)
        
        case publicKey
    }
    
    init(requestAPI: CustomAPI) {
        super.init()
        self.resuestAPI = requestAPI
        self.requestConfiguration()
    }
    private var resuestAPI: CustomAPI!
    
    override var path: String{
        switch self.resuestAPI {
        case .Upload:
            return "/BerryApi/fupload/upload"
        case .UploadImages:
            return "/BerryApi/fupload/uploadMulti"
        case .UploadVideo:
            return "/BerryApi/fupload/uploadVideo"
        case .Gifts:
            return "/BerryApi/gift"
        case .SendGift:
            return "/BerryApi/gift/deduction"
        case .fakeRequest:
            return "/BerryApi/\(randomString())/come\(randomString())Go"
        case .rechargeList:
            return "/BerryApi/recharge/listConfig"
        case .rechargeWithAppleCheck:
            return "/BerryApi/recharge/appleCheckBill"
        case .publicKey:
            return "/BerryApi/config/getCToken"
        default:
            return ""
        }
    }
    
    override var method: Moya.Method{
        switch self.resuestAPI {
        case .Upload,.UploadImages,.UploadVideo,.SendGift,.fakeRequest,.rechargeWithAppleCheck:
            return .post
        default:
            return .get
        }
    }
    
    override var task: Task{
        switch self.resuestAPI {
        case .Upload(let image,let file):
            if let data = image.jpegData(compressionQuality: 1.0) {
                let date:Date = Date()
                let formatter = DateFormatter()
                formatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
                let dateStr:String = formatter.string(from: date as Date)
                let formData = MultipartFormData(provider: .data(data), name: file, fileName: "\(dateStr).png", mimeType: "image/png")
                return .uploadMultipart([formData])
            }
            return .uploadMultipart([])
        case .UploadImages(let images):
            let formDatas = images.compactMap { image in
                if let data = image.jpegData(compressionQuality: 1.0) {
                    let date:Date = Date()
                    let formatter = DateFormatter()
                    formatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
                    let dateStr:String = formatter.string(from: date as Date)
                    let formData = MultipartFormData(provider: .data(data), name: "listFile", fileName: "\(dateStr).png", mimeType: "image/png")
                    return formData
                }
                return nil
            }
            return .uploadMultipart(formDatas)
        case .UploadVideo(let videoData):
            let date:Date = Date()
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
            let dateStr:String = formatter.string(from: date as Date)
            let formData = MultipartFormData(provider: .data(videoData), name: "file", fileName: "\(dateStr).MP4", mimeType: "video/mp4")
            return .uploadCompositeMultipart([formData], urlParameters: self.parameters)
        case .SendGift,.fakeRequest,.rechargeWithAppleCheck:
            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        default:
            if parameters.isEmpty == false{
                let parameStr = parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
                do {
                    let publicKey = try PublicKey(base64Encoded: XGL_EncipherTool.share.cpk)
                    let encryptedStr = try ClearMessage(string: parameStr, using: .utf8).encrypted(with: publicKey, padding: .PKCS1)
                    return .requestParameters(parameters: ["encryptParam": encryptedStr.base64String],encoding: parameterEncoding)
                } catch {
                print(error)
                }
            }
            return .requestParameters(parameters: parameters,encoding: parameterEncoding)
        }
    }
    
    override var cacheModel: PanRequestBaseAPI.PanRequestCacheModel{
        return .none
    }
    
    override var cacheKey: String{
        return "test"
    }
    
    override var parameters: [String : Any]{
        var dic = super.parameters
        switch self.resuestAPI {
        case .UploadVideo:
            dic["publishUserType"] = 2
            return dic
        case .Gifts:
            dic["page"] = 1
            dic["size"] = 100
            return dic
        case .SendGift(let giftId,let userId):
            dic["giftId"] = giftId
            dic["userId"] = userId
            return dic
        case .fakeRequest(let strContent,let intContent):
            if let value = strContent {
                dic["strContent"] = value
            }
            if let value = intContent {
                dic["intContent"] = value
            }
            return dic
        case .rechargeList:
            dic["type"] = 2
            dic["first"] = 0
            dic["pkgName"] = AppPackageName
            dic["rechargeType"] = 1
            return dic
        case .rechargeWithAppleCheck(let billNo):
            dic["pkgName"] = AppPackageName
            dic["billNo"] = billNo
            return dic
        default:
            return dic
        }
    }
}

